What is @floating-ui/utils?
The @floating-ui/utils package provides utility functions for positioning floating elements (like tooltips, popovers, dropdowns, etc.) in relation to reference elements in the DOM. It is part of the Floating UI library, which offers a low-level toolkit for creating floating elements.
What are @floating-ui/utils's main functionalities?
computePosition
This feature calculates the position of a floating element relative to a reference element and applies the calculated position to the floating element.
import {computePosition} from '@floating-ui/dom';
const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');
computePosition(referenceElement, floatingElement).then(data => {
Object.assign(floatingElement.style, {
left: `${data.x}px`,
top: `${data.y}px`,
});
});
arrow
The arrow utility positions an arrow element within a floating element so that it is properly aligned with the reference element.
import {computePosition, arrow} from '@floating-ui/dom';
const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');
const arrowElement = floatingElement.querySelector('#arrow');
computePosition(referenceElement, floatingElement, {
middleware: [arrow({element: arrowElement})],
}).then(data => {
Object.assign(floatingElement.style, {
left: `${data.x}px`,
top: `${data.y}px`,
});
Object.assign(arrowElement.style, data.middlewareData.arrow);
});
autoPlacement
The autoPlacement utility automatically chooses the best placement for a floating element based on available space in the viewport.
import {computePosition, autoPlacement} from '@floating-ui/dom';
const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');
computePosition(referenceElement, floatingElement, {
middleware: [autoPlacement()],
}).then(data => {
Object.assign(floatingElement.style, {
left: `${data.x}px`,
top: `${data.y}px`,
});
});
flip
The flip utility adjusts the placement of the floating element to prevent it from overflowing the viewport.
import {computePosition, flip} from '@floating-ui/dom';
const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');
computePosition(referenceElement, floatingElement, {
middleware: [flip()],
}).then(data => {
Object.assign(floatingElement.style, {
left: `${data.x}px`,
top: `${data.y}px`,
});
});
offset
The offset utility adds space between the reference element and the floating element, defined by the offset value.
import {computePosition, offset} from '@floating-ui/dom';
const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');
computePosition(referenceElement, floatingElement, {
middleware: [offset(10)],
}).then(data => {
Object.assign(floatingElement.style, {
left: `${data.x}px`,
top: `${data.y}px`,
});
});
Other packages similar to @floating-ui/utils
popper.js
Popper.js is a library used to create poppers in web applications. It is similar to @floating-ui/utils in that it provides positioning logic for tooltips, popovers, and other floating elements. Popper.js was the predecessor to the Floating UI library, which @floating-ui/utils is a part of.
tippy.js
Tippy.js is a highly customizable tooltip and popover library that wraps around Popper.js. It provides a higher-level abstraction over the positioning logic, making it easier to create tooltips and popovers with less setup compared to @floating-ui/utils.
tooltip.js
Tooltip.js is another library for creating and managing tooltips in web applications. It is built on top of Popper.js and provides a simpler API for creating tooltips, similar to Tippy.js. Compared to @floating-ui/utils, Tooltip.js offers less low-level control but is easier to use for common tooltip scenarios.